/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hormigas_packettracker.config;

import hormigas_packettracker.beans.Punto;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import hormigas_packettracker.beans.Vehiculo;

/**
 *
 * @author Shadow
 */
public class Grafico extends javax.swing.JFrame {

    /**
     * Creates new form Grafico
     */
    private Solucion solucion;

    public Solucion getSolucion() {
        return solucion;
    }

    public void setSolucion(Solucion solucion) {
        this.solucion = solucion;
    }

    public Grafico() {
        initComponents();
    }
    public double maxX, minX, maxY, minY;
    public double xCorr, yCorr, mX, mY;

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1022, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 682, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    @Override
    public void paint(Graphics graphics) {
        super.paint(graphics);
        Dimension d = getSize();
        minX = solucion.getMinX();
        maxX = solucion.getMaxX();
        xCorr = (maxX - minX) * 0.015;
        mX = minX - xCorr;
        minY = solucion.getMinY();
        maxY = solucion.getMaxY();
        yCorr = (maxY - minY) * 0.015;
        mY = minY - yCorr;
        double xFact = d.width / (maxX - minX + xCorr * 2);
        double yFact = d.height / (maxY - minY + yCorr * 2);
        Graphics2D g = (Graphics2D) graphics;
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setColor(Color.RED);
        if (solucion != null && !solucion.isEmpty()) {
            for (int i = 0; i < solucion.size(); ++i) {
                Vehiculo v = solucion.get(i);
                for (int j = 1; j < v.getRuta().size(); j++) {
                    Punto p2 = v.getRuta().get(j);
                    Punto p1 = v.getRuta().get(j - 1);
                    int x1, x2, y1, y2;
                    x1 = (int) Math.round((p1.getX() - mX) * xFact);
                    y1 = (int) Math.round((p1.getY() - mY) * yFact);
                    x2 = (int) Math.round((p2.getX() - mX) * xFact);
                    y2 = (int) Math.round((p2.getY() - mY) * yFact);
                    g.fillOval((int) Math.round((p1.getX() - mX) * xFact),
                            (int) Math.round((p1.getY() - mY) * yFact),
                            10, 4);
                    g.drawString(String.valueOf(i),
                            (int) Math.round((p1.getX() - mX) * xFact),
                            (int) Math.round((p1.getY() - mY) * yFact));
                    g.drawLine(x1, y1, x2, y2);
                }
                Punto p2 = v.getRuta().get(0);
                Punto p1 = v.getRuta().get(v.getRuta().size() - 1);
                int x1, x2, y1, y2;
                x1 = (int) Math.round((p1.getX() - mX) * xFact);
                y1 = (int) Math.round((p1.getY() - mY) * yFact);
                x2 = (int) Math.round((p2.getX() - mX) * xFact);
                y2 = (int) Math.round((p2.getY() - mY) * yFact);
                g.fillOval((int) Math.round((p1.getX() - mX) * xFact),
                        (int) Math.round((p1.getY() - mY) * yFact),
                        10, 4);
                g.drawString(String.valueOf(i),
                        (int) Math.round((p1.getX() - mX) * xFact),
                        (int) Math.round((p1.getY() - mY) * yFact));
                g.drawLine(x1, y1, x2, y2);
            }
        }
    }
    /**
     * @param args the command line arguments
     *
     * public static void main(String args[]) { /* Set the Nimbus look and feel
     */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     
    
     try {
     for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
     if ("Nimbus".equals(info.getName())) {
     javax.swing.UIManager.setLookAndFeel(info.getClassName());
     break;
     }
     }
     }
     catch (ClassNotFoundException ex

    
     ) {
     java.util.logging.Logger.getLogger(Grafico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
     }
     catch (InstantiationException ex

    
     ) {
     java.util.logging.Logger.getLogger(Grafico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
     }
     catch (IllegalAccessException ex

    
     ) {
     java.util.logging.Logger.getLogger(Grafico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
     }
     catch (javax.swing.UnsupportedLookAndFeelException ex

    
     ) {
     java.util.logging.Logger.getLogger(Grafico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
     }
     //</editor-fold>

     /* Create and display the form 
     java.awt.EventQueue.invokeLater(new Runnable() {
     public void run() {
     new Grafico().setVisible(true);
     }
     });
     }*/
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
